<template>
  <div class="mod-user">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="dataForm.serialNo"
          placeholder="编号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div class="block"></div>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button type="primary"
          ><i class="el-icon-printer"></i>打印</el-button
        > -->
      </el-form-item>
      <el-form-item>
        <em>本月发生总额:{{ allCurrentAmount }}</em>
        <em>当前总余额:{{ allMonthAmount }}</em>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 70%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        prop="serialNo"
        header-align="center"
        align="center"
        label="编号"
      >
      </el-table-column>
      <el-table-column
        prop="initialAmount"
        header-align="center"
        align="center"
        label="期初金额"
      >
      </el-table-column>
      <el-table-column
        prop="thisMonthAmount"
        header-align="center"
        align="center"
        width="180"
        label="本月发生额"
      >
      </el-table-column>
      <el-table-column
        prop="currentAmount"
        header-align="center"
        align="center"
        label="当前余额"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sys:user:update')"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id,scope.row.initialAmount)"
            >查看流水详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./account-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        name: "",
        serialNo: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      allCurrentAmount: "",
      allMonthAmount: "",
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
    this.getAmount();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let currentPage = this.pageIndex || 1;
      let pageSize = this.pageSize || 10;
      let str = `{"name":"${this.dataForm.name}","serialNo":"${this.dataForm.serialNo}","remark":""}`;
      var search = encodeURIComponent(str);
      this.$http({
        url: this.$http.adornUrl(`/account/list?search=${search}`),
        method: "get",
        params: this.$http.adornParams({
          currentPage: this.pageIndex,
          pageSize: this.pageSize,
        },false),
      }).then(({ data }) => {
         console.log(data.data)
        if (data && data.code === 0) {
          this.dataList = data.data;
          // this.totalPage = data.data.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },

    // 获取余额信息
    getAmount() {
      this.$http({
        url: this.$http.adornUrl("/account/getStatistics"),
        method: "get",
      }).then(({ data }) => {
        // console.log(data.data);
        if (data && data.code === 0) {
          this.allCurrentAmount = data.data.allCurrentAmount;
          this.allMonthAmount = data.data.allMonthAmount;
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id,initialAmount) {
      // console.log(id,initialAmount)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id,initialAmount);
      });
    },
    // 删除
    deleteHandle(id) {
      var userIds = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.userId;
          });
      this.$confirm(
        `确定对[id=${userIds.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sys/user/delete"),
            method: "post",
            data: this.$http.adornData(userIds, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },

    // 重置
    resetDataList() {
      // console.log(11)
      this.dataForm.userName = null;
      this.dataForm.userID = null;
    },
  },
};
</script>
